package com.ruoyi.tzai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tzai.domain.AIModelConfig;
import com.ruoyi.tzai.service.IAIModelConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * AI模型配置Controller
 * 
 * @author tzai
 * @date 2025-10-27
 */
@Api(tags = "AI模型配置")
@RestController
@RequestMapping("/tzai/ai/model")
public class AIModelConfigController extends BaseController
{
    @Autowired
    private IAIModelConfigService aiModelConfigService;

    @Autowired
    private com.ruoyi.tzai.service.ai.AIServiceFactory aiServiceFactory;

    /**
     * 查询AI模型配置列表
     */
    @ApiOperation("查询AI模型配置列表")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(AIModelConfig aiModelConfig)
    {
        startPage();
        List<AIModelConfig> list = aiModelConfigService.selectAIModelConfigList(aiModelConfig);
        return getDataTable(list);
    }

    /**
     * 导出AI模型配置列表
     */
    @ApiOperation("导出AI模型配置列表")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:export')")
    @Log(title = "AI模型配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AIModelConfig aiModelConfig)
    {
        List<AIModelConfig> list = aiModelConfigService.selectAIModelConfigList(aiModelConfig);
        ExcelUtil<AIModelConfig> util = new ExcelUtil<AIModelConfig>(AIModelConfig.class);
        util.exportExcel(response, list, "AI模型配置数据");
    }

    /**
     * 测试AI模型连接
     */
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:test')")
    @Log(title = "AI模型配置", businessType = BusinessType.OTHER)
    @GetMapping("/test/{modelId}")
    public AjaxResult testConnection(@PathVariable("modelId") Long modelId)
    {
        com.ruoyi.tzai.service.ai.IAIService aiService = aiServiceFactory.getService(modelId);
        if (aiService == null) {
            return error("无法创建AI服务实例");
        }
        
        boolean result = aiService.testConnection();
        return result ? success("连接测试成功") : error("连接测试失败");
    }

    /**
     * 清除AI模型缓存
     */
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:edit')")
    @Log(title = "AI模型配置", businessType = BusinessType.UPDATE)
    @DeleteMapping("/cache/{modelId}")
    public AjaxResult clearCache(@PathVariable("modelId") Long modelId)
    {
        aiServiceFactory.clearCache(modelId);
        return success("缓存清除成功");
    }

    /**
     * 清除所有AI模型缓存
     */
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:edit')")
    @Log(title = "AI模型配置", businessType = BusinessType.UPDATE)
    @DeleteMapping("/cache")
    public AjaxResult clearAllCache()
    {
        aiServiceFactory.clearAllCache();
        return success("所有缓存已清除");
    }

    /**
     * 获取AI模型配置详细信息
     */
    @ApiOperation("获取AI模型配置详细信息")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId)
    {
        return success(aiModelConfigService.selectAIModelConfigByModelId(modelId));
    }

    /**
     * 获取默认AI模型配置
     */
    @ApiOperation("获取默认AI模型配置")
    @GetMapping(value = "/default")
    public AjaxResult getDefault()
    {
        return success(aiModelConfigService.selectDefaultAIModelConfig());
    }

    /**
     * 根据模型代码获取AI模型配置
     */
    @ApiOperation("根据模型代码获取AI模型配置")
    @GetMapping(value = "/code/{modelCode}")
    public AjaxResult getInfoByCode(@PathVariable("modelCode") String modelCode)
    {
        return success(aiModelConfigService.selectAIModelConfigByModelCode(modelCode));
    }

    /**
     * 新增AI模型配置
     */
    @ApiOperation("新增AI模型配置")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:add')")
    @Log(title = "AI模型配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AIModelConfig aiModelConfig)
    {
        return toAjax(aiModelConfigService.insertAIModelConfig(aiModelConfig));
    }

    /**
     * 修改AI模型配置
     */
    @ApiOperation("修改AI模型配置")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:edit')")
    @Log(title = "AI模型配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AIModelConfig aiModelConfig)
    {
        return toAjax(aiModelConfigService.updateAIModelConfig(aiModelConfig));
    }

    /**
     * 删除AI模型配置
     */
    @ApiOperation("删除AI模型配置")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:remove')")
    @Log(title = "AI模型配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds)
    {
        return toAjax(aiModelConfigService.deleteAIModelConfigByModelIds(modelIds));
    }

    /**
     * 设置为默认模型
     */
    @ApiOperation("设置为默认模型")
    @PreAuthorize("@ss.hasPermi('tzai:ai:model:edit')")
    @Log(title = "AI模型配置", businessType = BusinessType.UPDATE)
    @PutMapping("/setDefault/{modelId}")
    public AjaxResult setDefault(@PathVariable Long modelId)
    {
        return toAjax(aiModelConfigService.setAsDefault(modelId));
    }

}

